package main.ressik;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class FoodListView{

		private FoodListController flc;
		private Main main;
		private ListView list;
		
		
		
		
		
		/**
		 * Constructor
		 */
		public FoodListView(){
			main = Main.getInstance();
			flc = new FoodListController();
		}
		
		
		
		
		
		/**
		 * Refreshes or builds the list of foods that have been added.  
		 * This needs to be done because the API doesn't automatically
		 * update this list.  This method is called whenever a food item is added,
		 * update, or removed.
		 */
	   @SuppressWarnings("unchecked")
		public void buildList(){
		    list = (ListView)Main.getInstance().findViewById(R.id.addedFoodsTab);
		   	ListAdapter lAdapter = new ArrayAdapter(main,android.R.layout.simple_list_item_1,flc.getFoodsInDb());
	    	list.setAdapter(lAdapter);
			list.setTextFilterEnabled(true);
			list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
				public void onItemClick(AdapterView av,View v, int position, long id){
					flc.listViewClick(id, main,list);
				}
			});
			list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
				public boolean onItemLongClick(AdapterView av, View v, int position, long id){
					int newId = (int) id;
					final String food = (String) av.getItemAtPosition(newId);
					flc.listViewLongClick(food, main, main.getFood_text(), main.getCal_txt(), main.getFat_txt(), main.getFiber_txt(), main.getAddFood_btn(),FoodListView.this);
					return true;
				}
			});
	    }
	   
	   
	   
	   
	   /**
	    * Returns the FoodListController Object
	    */
	   public FoodListController getFoodListController(){
		   return flc;
	   }
	   
	   
	   
}
